//
//  FYNetworkingDeallocClearnRequestOperation.m
//  FYNetworking
//
//  Created by Felix Yin on 2017/5/5.
//  Copyright © 2017年 Felix Yin. All rights reserved.
//

#import "FYNetworkingDeallocClearnRequestOperation.h"
#import "FYNetworkingRequestUnit.h"

@interface FYNetworkingDeallocClearnRequestOperation()

@property (nonatomic, strong) NSMutableDictionary<NSNumber *,FYNetworkingRequestUnit *> *requestCache;

@end

@implementation FYNetworkingDeallocClearnRequestOperation

//request 始发object dealloc 时清空所有缓存请求
- (void)dealloc{
    [self.requestCache enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, FYNetworkingRequestUnit * _Nonnull obj, BOOL * _Nonnull stop) {
        [obj cancleRequest];
    }];
    [self.requestCache removeAllObjects];
    self.requestCache = nil;
}

- (void)cacheRequestUnit:(FYNetworkingRequestUnit *)requestUnit requestID:(NSNumber *)requestID{
    if (requestUnit && requestID) {
        [self.requestCache setObject:requestUnit forKey:requestID];
    }
}

- (void)cleanRequestUnitWithRequestID:(NSNumber *)requestID{
    if (requestID) {
        [self.requestCache removeObjectForKey:requestID];
    }
}

#pragma mark LazyLoading
- (NSMutableDictionary *)requestCache{
    if (!_requestCache) {
        _requestCache = [[NSMutableDictionary alloc] init];
    }
    return _requestCache;
}

@end
